#include <stdio.h>  
#include <stdlib.h>  
#include <assert.h>  
  
// 媒婆  
typedef struct _Mediator {  
    struct _People* man;  
    struct _People* woman;  
} Mediator;  
  
// 参与者  
typedef struct _People {  
    Mediator* pMediator;  
    void (*request)(struct _People* pPeople);  
    void (*process)(struct _People* pReceiver);  
} People;  
  
// 男人和女人的具体行为  
void man_request(struct _People* pPeople) {  
    printf("Man makes a request.\n");  
    if (pPeople->pMediator && pPeople->pMediator->woman) {  
        pPeople->pMediator->woman->process(pPeople->pMediator->woman);  
    }  
}  
  
void man_process(struct _People* pReceiver) {  
    printf("Man receives a process from Woman.\n");  
}  
  
void woman_request(struct _People* pPeople) {  
    printf("Woman makes a request.\n");  
    if (pPeople->pMediator && pPeople->pMediator->man) {  
        pPeople->pMediator->man->process(pPeople->pMediator->man);  
    }  
}  
  
void woman_process(struct _People* pReceiver) {  
    printf("Woman receives a process from Man.\n");  
}  
  
// 初始化媒婆和参与者  
Mediator* createMediator() {  
    Mediator* mediator = (Mediator*)malloc(sizeof(Mediator));  
    if (!mediator) {  
        exit(EXIT_FAILURE);  
    }  
    mediator->man = NULL;  
    mediator->woman = NULL;  
    return mediator;  
}  
  
People* createPeople(void (*requestFunc)(struct _People*), void (*processFunc)(struct _People*)) {  
    People* person = (People*)malloc(sizeof(People));  
    if (!person) {  
        exit(EXIT_FAILURE);  
    }  
    person->request = requestFunc;  
    person->process = processFunc;  
    person->pMediator = NULL;  
    return person;  
}  
  
// 示例用法  
int main() {  
    Mediator* mediator = createMediator();  
    People* man = createPeople(man_request, man_process);  
    People* woman = createPeople(woman_request, woman_process);  
  
    // 绑定媒婆和参与者  
    mediator->man = man;  
    mediator->woman = woman;  
    man->pMediator = mediator;  
    woman->pMediator = mediator;  
  
    // 模拟交互  
    man->request(man);  
    woman->request(woman);  
  
    // 清理资源  
    free(man);  
    free(woman);  
    free(mediator);  
  
    return 0;  
}